package cn.hl.hm.service.impl;

import cn.hl.hm.dao.DriverMapper;
import cn.hl.hm.dao.SecurityFeeTypeHistoryMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.SecurityFeeTypeHistory;
import cn.hl.hm.service.SecurityFeeTypeHistoryService;
import com.vizhuo.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SecurityFeeTypeHistoryServiceImpl implements SecurityFeeTypeHistoryService {
    @Autowired
    private SecurityFeeTypeHistoryMapper securityFeeTypeHistoryMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public int deleteByPrimaryKey(Integer id){
        return securityFeeTypeHistoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SecurityFeeTypeHistory record){
        return securityFeeTypeHistoryMapper.insert(record);
    }

    @Override
    public int insertSelective(SecurityFeeTypeHistory record){
        return securityFeeTypeHistoryMapper.insertSelective(record);
    }

    @Override
    public SecurityFeeTypeHistory selectByPrimaryKey(Integer id){
        return securityFeeTypeHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SecurityFeeTypeHistory record){
        return securityFeeTypeHistoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SecurityFeeTypeHistory record){
        return securityFeeTypeHistoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int save(Driver driverAfter, String afterType,Integer updateBy) {
        SecurityFeeTypeHistory securityFeeTypeHistory =new SecurityFeeTypeHistory();
        securityFeeTypeHistory.setCreateTime(new Date());

        Driver driverBefore=new Driver();
        driverBefore.setDriverAccount(driverAfter.getDriverAccount());
        List<Driver> drivers=driverMapper.selectDriver(driverBefore);
        if (drivers!=null) {
            driverBefore=drivers.get(0);
            //        //无修改不记录操作历史
//        if(driverAfter.getNoloadFree().equals(driverBefore.getNoloadFree()) &&
//                driverAfter.getIsSecurity().equals(driverBefore.getIsSecurity()) &&
//                driverAfter.getFreeTime()==driverBefore.getFreeTime()){
//            return 1;
//        }else{
            securityFeeTypeHistory.setDriverId(driverBefore.getId());
            securityFeeTypeHistory.setUpdateBy(updateBy);
            securityFeeTypeHistory.setTypeBefore(getType(driverBefore));
            securityFeeTypeHistory.setTypeAfter(getType(driverAfter));
            return insertSelective(securityFeeTypeHistory);
//        }
        }else{
            return 0;
        }

    }

    @Override
    public List<SecurityFeeTypeHistory> selectListByPrimaryKeySelective(Integer driverId, String startTime, String endTime, Integer updateBy) {
        return securityFeeTypeHistoryMapper.selectListByPrimaryKeySelective(driverId, startTime, endTime, updateBy);
    }

    @Override
    public List<Integer> selectUpdateBys() {
        return securityFeeTypeHistoryMapper.selectUpdateBys();
    }

    private String getType(Driver driver) {
        if (driver.getNoloadFree()!=null && driver.getNoloadFree().equals(1)) {
            return ("空载免收");
        }else{
            if(driver.getIsSecurity().equals("0")) {
                if(driver.getFreeTime()!=null) {
                    SimpleDateFormat hhmmSdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String format = hhmmSdf.format(new Date());
                    Date nowDate = DateUtil.string2DateNoTime(format);
                    String format2 = hhmmSdf.format(driver.getFreeTime());
                    Date driverDate =DateUtil.string2DateNoTime(format2);
                    if(driverDate.getTime()>=nowDate.getTime()) {
                        return ("免费至"+ DateUtil.date2StringNoTime(driver.getFreeTime()));
                    }else {
                        return ("永久收费");
                    }
                }else {
                    return ("永久收费");
                }
            }else {
                return ("永久免费");
            }
        }
    }
}